<?php
/*********************/
/*                   */
/*  前台验证邮箱     */
/*   BY voiet        */
/*   2009-8-18       */
/*                   */
/*********************/

class cct_passport extends ctl_passport
{

	function create( )
	{
		$account = $this->system->loadmodel( "member/account" );
		$passport = $this->system->loadmodel( "member/passport" );
		if ( $obj = $passport->function_judge( "getPlugCookie" ) )
		{
			if ( $ck = $obj->getplugcookie( ) )
			{
				$common = 1;
			}
			if ( $_REQUEST['plugUrl'] )
			{
				$common = 0;
			}
			else if ( $obj->getplugcookie( "Common" ) )
			{
				$common = 0;
			}
			else
			{
				$common = 1;
			}
		}
		else
		{
			$common = 1;
		}
		if ( $common )
		{
			if ( ( $this->system->getconf( "site.register_valide" ) == "true" || $this->system->getconf( "site.register_valide" ) == true ) && $_COOKIE['S_RANDOM_CODE'] != md5( $_POST['signupverifycode'] ) )
			{
				$this->splash( "failed", "back", "验证码录入错误，请重新输入" );
			}
			$passwdLen = strlen( $_POST['passwd'] );
			if ( $_POST['license'] != "agree" )
			{
				$this->splash( "failed", "back", __( "同意注册条款后才能注册" ) );
			}
			else if ( !preg_match( "/\\S+@\\S+/", $_POST['email'] ) )
			{
				$this->splash( "failed", "back", __( "邮件格式不正确" ) );
			}
			else if ( $passwdLen < 4 )
			{
				$this->splash( "failed", "back", __( "密码长度不能小于4" ) );
			}
			else if ( 20 < $passwdLen )
			{
				$this->splash( "failed", "back", __( "密码长度不能大于20" ) );
			}
			if ( $sCheck = $account->checkmember( $_POST ) )
			{
				$sError = __( "该用户名已经存在" );
				$this->splash( "failed", "back", $sError );
				exit( );
			}
			else
			{
				$pObj = $this->system->loadmodel( "member/passport" );
				if ( $obj = $pObj->function_judge( "checkuserregister" ) )
				{
					$sRes = $obj->checkuserregister( $_POST['uname'], $_POST['passwd'], $_POST['email'], $uid, $message );
					if ( $sRes )
					{
						if ( !empty( $message ) )
						{
							$this->splash( "failed", "back", $message );
							exit( );
						}
						else
						{
							if ( $_REQUEST['forward'] )
							{
								$url = $_REQUEST['forward'].DEFAULT_INDEX;
							}
							else
							{
								$url = $this->system->request['base_url'].DEFAULT_INDEX;
							}
							if ( $info = $account->createuserfrompluin( $_POST, $message, $uid ) )
							{
								$this->system->setcookie( "MEMBER", $info['secstr'], null );
								$this->system->setcookie( "UNAME", $info['uname'], null );
								$this->system->setcookie( "MLV", $info['member_lv_id'], null );
								$GLOBALS['runtime']['member_lv'] = $info['member_lv_id'];
								$this->system->setcookie( "CUR", $info['cur'], null );
								$this->system->setcookie( "LANG", $info['lang'], null );
								$oPassport = $this->system->loadmodel( "member/passport" );
								$loginfo = $oPassport->login( $info['member_id'], $url );
								$this->header .= $loginfo;
								
						
						//注册送10元
						if(time()>=mktime(0,0,0,11,5,2009)){
							$oCoupon = $this->system->loadmodel( "trading/coupon" );
							$oCoupon->generatecoupon( 41, $info['member_id'], 1 );
						}
						
						//2009-9-21 注册送积分
						$starttime = mktime(0,0,0,9,22,2009);
						$endtime = mktime(0,0,0,11,1,2009);
						if(time()>=$starttime && time()<=$endtime){
								//2009-9-21 推荐送积分 排除IP
								$regip=remote_addr();
								if(!empty($refer_id)){
									$rRefer = explode("|",$refer_id);
									if($rRefer[0]=='prem' && intval($rRefer[1])!=0){
										if($account->checkregip($regip)){
											$regpoint = $this->system->loadmodel( "trading/memberPoint" );
											$regpoint->chgpointall( $info['member_id'], 500, 'register_mem', $info['member_id'] );
											$regpoint->chgpointall( $rRefer[1], 100, 'recommend_mem', $info['member_id'] );
											$account->updatecpa($info['member_id'],5);
										}
									}
								
								}
								
						}
								
								if ( $_POST['regType'] == "buy" )
								{
									$oCart = $this->system->loadmodel( "trading/cart" );
									$cartCookie = $oCart->getcart( );
									$oCart->checkmember( $info );
									$oCart->save( "all", $cartCookie );
									$this->system->setcookie( $oCart->cookiesName, "" );
									$this->splash( "success", $this->system->mkurl( "cart", "checkout" ), __( "注册成功，并进入购买" ), "", 1 );
								}
							}
							else
							{
								$this->splash( "failed", "back", $message );
							}
						}
					}
				}
				else
				{
					if ( $_REQUEST['forward'] )
					{
						$url = $_REQUEST['forward'].DEFAULT_INDEX;
					}
					else
					{
						$url = $this->system->request['base_url'].DEFAULT_INDEX;
					}
					if ( $info = $account->create( $_POST, $message ) )
					{
						$this->system->setcookie( "MEMBER", $info['secstr'], null );
						$this->system->setcookie( "UNAME", $info['uname'], null );
						$this->system->setcookie( "MLV", $info['member_lv_id'], null );
						$GLOBALS['runtime']['member_lv'] = $info['member_lv_id'];
						$this->system->setcookie( "CUR", $info['cur'], null );
						$this->system->setcookie( "LANG", $info['lang'], null );
						
						
						//注册送10元
						if(time()>=mktime(0,0,0,11,5,2009)){
							$oCoupon = $this->system->loadmodel( "trading/coupon" );
							$oCoupon->generatecoupon( 41, $info['member_id'], 1 );
						}
						//2009-9-21 推荐注册送积分
						$starttime = mktime(0,0,0,9,22,2009);
						$endtime = mktime(0,0,0,11,1,2009);
						if(time()>=$starttime && time()<=$endtime){
								
								//2009-9-21 推荐送积分 排除IP
								$regip=remote_addr();
								if(!empty($refer_id)){
									$rRefer = explode("|",$refer_id);
									if($rRefer[0]=='prem' && intval($rRefer[1])!=0){
									
										if($account->checkregip($regip)){
											$regpoint = $this->system->loadmodel( "trading/memberPoint" );
											$regpoint->chgpointall( $info['member_id'], 500, 'register_mem', $info['member_id'] );
											$regpoint->chgpointall( $rRefer[1], 100, 'recommend_mem', $info['member_id'] );
											$account->updatecpa($info['member_id'],5);
										}
									}
								
								}
								
						}
					
						if ( $_POST['regType'] == "buy" )
						{
							$oCart = $this->system->loadmodel( "trading/cart" );
							$cartCookie = $oCart->getcart( );
							$oCart->checkmember( $info );
							$oCart->save( "all", $cartCookie );
							$this->system->setcookie( $oCart->cookiesName, "" );
							$this->splash( "success", $this->system->mkurl( "cart", "checkout" ), __( "注册成功，并进入购买" ), "", 1 );
						}
						else
						{
							$oPassport = $this->system->loadmodel( "member/passport" );
							if ( $opbj = $oPassport->function_judge( "setPlugCookie" ) )
							{
								$opbj->setplugcookie( 0 );
							}
							if ( $oPassport->forward )
							{
								$oPassport->regist( $info['member_id'], $url );
							}
							else
							{
								if ( $_POST['forward'] )
								{
									$_REQUEST['forward'].DEFAULT_INDEX;
								}
								$oPassport->regist( $info['member_id'], $url );
							}
						}
					}
					else
					{
						$this->splash( "failed", "back", $message );
					}
				}
			}
		}
		else if ( $obj = $passport->function_judge( "setPlugCookie" ) )
		{
			$obj->setplugcookie( 0 );
		}
		$oMem = $this->system->loadmodel( "member/member" );
		$mematt = $this->system->loadmodel( "member/memberattr" );
		$filter['attr_show'] = "true";
		$tmpdate = $mematt->getlist( "*", $filter, 0, -1, $count, array( "attr_order", "asc" ) );
		$i = 0;
		for ( ;	$i < count( $tmpdate );	++$i	)
		{
			if ( $tmpdate[$i]['attr_type'] == "select" || $tmpdate[$i]['attr_type'] == "checkbox" )
			{
				$tmpdate[$i]['attr_option'] = unserialize( $tmpdate[$i]['attr_option'] );
			}
		}
		$this->pagedata['tree'] = $tmpdate;
		$this->pagedata['plugUrl'] = $_REQUEST['plugUrl'];
		$this->output( );
	}
	
	
	function verifyemail()
	{
			$message = "邮箱验证成功。";
			if(empty($_GET['uname'])){
				$message = "错误的地址";
			}
			else{
				$oVerifyEmail = $this->system->loadmodel( "member/account" );
				$row = $oVerifyEmail->getrefer($_GET['uname']);
				$referinfo = explode('|',$row['refer_id']);
				if($row['cpatype'] != 1){
					if($referinfo[0] == 'pceggscpa'){
				
					//给pcegg反馈信息
					}
					if(!$oVerifyEmail->updatecpa($row['member_id'],1)){
						$message = "邮箱验证失败。";
					}
				
				}
				else {
					$message = "邮箱已验证过。";
			
				}
			}
			$this->pagedata['message'] = $message;
			$this->output( );					
	}
	
	function emailcheck( )
	{
		$member = $this->system->loadmodel( "member/account" );
		$email = trim( $_POST['email'] );
		if ( $member->check_email( $email, $message ) )
		{
			echo "<div class=\"valisuccess\">&nbsp;<strong></strong>&nbsp;OK&nbsp;</div>";
		}
		else
		{
			echo "<div class=\"valierror\">&nbsp;<strong></strong>&nbsp;".$message."</div>";
		}
	}
	
	function pfsignup( $url, $forward = "" )
	{
		$passport = $this->system->loadmodel( "member/passport" );
		if ( $obj = $passport->function_judge( "ServerClient" ) )
		{
			$type = $obj->serverclient( "signup" );
		}
		$this->showvalidecode( "signup" );
		switch ( $url )
		{
		case "checkout" :
			$options['url'] = $this->system->mkurl( "cart", "checkout" );
		}
		$this->title = "批发注册";
		$this->pagedata['options'] = $options;
		$this->pagedata['forward'] = $_REQUEST['forward'];
		$this->output( );
	}
	
	function czsignup( $url, $forward = "" )
	{
		$passport = $this->system->loadmodel( "member/passport" );
		if ( $obj = $passport->function_judge( "ServerClient" ) )
		{
			$type = $obj->serverclient( "signup" );
		}
		$this->showvalidecode( "signup" );
		switch ( $url )
		{
		case "checkout" :
			$options['url'] = $this->system->mkurl( "cart", "checkout" );
		}
		$this->title = "注册";
		$this->pagedata['options'] = $options;
		$this->pagedata['forward'] = $_REQUEST['forward'];
		$this->output( );
	}
	
	
	function createwholesale( )
	{
		$account = $this->system->loadmodel( "member/account" );
		$passport = $this->system->loadmodel( "member/passport" );
		
		if ( ( $this->system->getconf( "site.register_valide" ) == "true" || $this->system->getconf( "site.register_valide" ) == true ) && $_COOKIE['S_RANDOM_CODE'] != md5( $_POST['signupverifycode'] ) )
		{
			$this->splash( "failed", "back", "验证码录入错误，请重新输入" );
		}
		$passwdLen = strlen( $_POST['passwd'] );
		if ( $_POST['license'] != "agree" )
		{
			$this->splash( "failed", "back", __( "同意注册条款后才能注册" ) );
		}
		else if ( !preg_match( "/\\S+@\\S+/", $_POST['email'] ) )
		{
			$this->splash( "failed", "back", __( "邮件格式不正确" ) );
		}
		else if ( $passwdLen < 4 )
		{
			$this->splash( "failed", "back", __( "密码长度不能小于4" ) );
		}
		else if ( 20 < $passwdLen )
		{
			$this->splash( "failed", "back", __( "密码长度不能大于20" ) );
		}
		if($_POST['pifatype'] == 0 )
		{
			$this->splash( "failed", "back", "请选择批发类型" );
		}
		if($_POST['shenfen_name'] == "" || $_POST['shenfen_addr'] == "" || $_POST['shenfen_num'] == "" || $_POST['mobile'] == "" )
		{
		
			$this->splash( "failed", "back", "信息填写不完整");
		
		}
		/*
		if($_FILES['sf_file']['name'] == '')
		{
			$this->splash( "failed", "back", "请选择上传身份证照片或扫描件" );
		}
		
		*/
		
		if($_POST['pifatype'] == 1 || $_POST['pifatype'] == 3 || $_POST['pifatype'] == 5 || $_POST['pifatype'] == 7)
		{
			if($_POST['wangdian_url'] == "" || $_POST['wangdian_id'] == "")$this->splash( "failed", "back", "申请网店需要店铺地址和网店用户名" );
		
		}
		if($_POST['pifatype'] == 2 || $_POST['pifatype'] == 3 || $_POST['pifatype'] == 6 || $_POST['pifatype'] == 7)
		{
			if($_POST['shiti_addr'] == "")$this->splash( "failed", "back", "申请实体店需要实体店铺地址" );
			//if($_FILES['yy_file']['name'] == '')$this->splash( "failed", "back", "申请实体店需要营业执照或租赁合同扫描件" );
		
		}
		
		if ( $sCheck = $account->checkmember( $_POST ))
		{
			$sError = __( "该用户名已经存在" );
			$this->splash( "failed", "back", $sError );
			exit( );
		}
		else
		{
			$sf_upload_file = "";
			$yy_upload_file = "";
			if ( 512000 < $_FILES['sf_file']['size'] || 512000 < $_FILES['yy_file']['size'])
			{
				$this->splash( "failed", "back", "上传文件不能超过500k" );
			}	
			if ( $_FILES['sf_file']['name'] != "" )
			{
				$type = array( "jpg", "gif", "jpeg" );
				if ( !in_array( strtolower( $this->fileext( $_FILES['sf_file']['name'] ) ), $type ) )
				{
					$text = implode( ",", $type );
					$this->splash( "failed", "back", "您只能上传以下类型文件: ".$text."<br>" );
				}
				$sf_file_type = strtolower( $this->fileext( $_FILES['sf_file']['name'] ) );
				$sf_file_path = HOME_DIR."/upload/sf/";
				$sf_file_name = "sf_".time( ).rand( 0, 15 );
				$sf_upload_file = $sf_file_path.$sf_file_name.".".$sf_file_type;
				if ( move_uploaded_file( $_FILES['sf_file']['tmp_name'], $sf_upload_file ) )
				{
					$sf_upload_file = realpath( $sf_upload_file );
				}
			}
		
			if ( $_FILES['yy_file']['name'] != "" )
			{
				$type = array( "jpg", "gif", "jpeg" );
				if ( !in_array( strtolower( $this->fileext( $_FILES['yy_file']['name'] ) ), $type ) )
				{
					$text = implode( ",", $type );
					$this->splash( "failed", "back", "您只能上传以下类型文件: ".$text."<br>" );
				}
				$yy_file_type = strtolower( $this->fileext( $_FILES['yy_file']['name'] ) );
				$yy_file_path = HOME_DIR."/upload/yy/";
				$yy_file_name = "sf_".$this->member['member_id']."_".time( ).rand( 0, 15 );
				$yy_upload_file = $yy_file_path.$yy_file_name.".".$yy_file_type;
				if ( move_uploaded_file( $_FILES['yy_file']['tmp_name'], $yy_upload_file ) )
				{
					$yy_upload_file = realpath( $yy_upload_file );
				}
			}
		
			if ( $_REQUEST['forward'] )
			{
				$url = $_REQUEST['forward'].DEFAULT_INDEX;
			}
			else
			{
				$url = $this->system->request['base_url'].DEFAULT_INDEX;
			}
			if ( $info = $account->create( $_POST, $message ) )
			{
				$this->system->setcookie( "MEMBER", $info['secstr'], null );
				$this->system->setcookie( "UNAME", $info['uname'], null );
				$this->system->setcookie( "MLV", $info['member_lv_id'], null );
				$GLOBALS['runtime']['member_lv'] = $info['member_lv_id'];
				$this->system->setcookie( "CUR", $info['cur'], null );
				$this->system->setcookie( "LANG", $info['lang'], null );
				
				$pifaobj = $this->system->loadmodel( "member/pifa" );
		
				if($pifaobj->check_sf_no($_POST['shenfen_num']) || $pifaobj->check_userid($info['member_id']))
				{
					$this->splash( "failed", "back", "您已申请过批发会员，请勿再次申请。" );
				}
				if($info['member_lv_id'] >= 4 && $info['member_lv_id']!=17)
				{
					$this->splash( "failed", "back", "您已是批发会员，请勿再次申请。" );
		
				}
		
				if(trim($_POST['source']) != '')$aData['source'] =trim($_POST['source']);
				else $aData['source'] = 'cpa';
				$aData['pifatype'] = $_POST['pifatype'];
				$aData['regtime'] = time( );
				$aData['shenfen'] = $sf_file_name.".".$sf_file_type;
				$aData['yingye'] = $yy_file_name.".".$yy_file_type;
				$aData['userid'] = $info['member_id'];
				$aData['username'] = $info['uname'];
				$aData['wangdian_id'] = $_POST['wangdian_id'];
				$aData['truename'] = $_POST['shenfen_name'];
				$aData['address'] = $_POST['shenfen_addr'];
				$aData['sf_no'] = $_POST['shenfen_num'];
				$aData['mobile'] = $_POST['mobile'];
				$aData['qq'] = $_POST['qq'];
				$aData['wangwang'] = $_POST['ww'];
				$aData['online_addr'] = $_POST['wangdian_url'];
				$aData['entity_addr'] = $_POST['shiti_addr'];
				$aData['ip'] = $_SERVER['REMOTE_ADDR'];
				$aData['status'] = 1;
				$aData['ifsub'] = 1;
			
				if ( $pifaobj->save( $aData ) )
				{
					$this->splash( "success", $this->system->mkurl( "index", "home" ), __( "提交成功，请联系客服审核开通。" ), "", 1 );
					//$this->splash( "success", $this->system->mkurl( "pifa", "pfsucess" ), "提交成功" );
					//$this->redirect( "pifa", "pfadd" );
				}
				else
				{
					$this->splash( "failed", "back", "申请批发资料提交失败" );
				}
						
				
			}
			else
			{
				$this->splash( "failed", "back", $message );
			}
		}
	
		
		//$oMem = $this->system->loadmodel( "member/member" );
		
		//$this->pagedata['tree'] = $tmpdate;
		$this->pagedata['plugUrl'] = $_REQUEST['plugUrl'];
		$this->output( );
	}
	

}

?>